#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int w[N][N];
int dis[N], lever[N];
bool st[N];
int m, n;
int dijkstra(int down, int up)
{
	memset(dis, 0x3f, sizeof(dis));
	memset(st, 0, sizeof(st));
	dis[0] = 0;
	for (int i = 1; i <= n + 1; i++) {
		int t = -1;
		for (int j = 0; j <= n; j++) {
			if (!st[j] && (t == -1 || dis[t] > dis[j])) {
				t = j;
			}
		}
		st[t] = 1;
		for (int j = 0; j <= n; j++) {
			if (lever[j] >= down && lever[j] <= up) {
				dis[j] = min(dis[j], dis[t] + w[t][j]);
			}
		}
	}
	return dis[1];
}
int main()
{
	cin >> m >> n;
	memset(w, 0x3f, sizeof(w));
	for (int i = 1; i <= n; i++) {
		w[i][i] = 0;
	}
	for (int i = 1; i <= n; i++) {
		int price, cnt;
		cin >> price >> lever[i] >> cnt;
		w[0][i] = min(w[0][i], price);
		while (cnt--) {
			int id, cost;
			cin >> id >> cost;
			w[id][i] = min(w[id][i], cost);
		}
	}
	int res = INF;
	for (int i = lever[1] - m; i <= lever[1]; i++) {
		res = max(res, dijkstra(i, i + m));
	}
	cout << res << endl;
	return 0;
}